/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.label;

import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import org.jxUtils.image.ImageUtil;
import org.jxUtils.system.OperatingSystem;
import org.jxUtils.system.SystemProperties;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.label
 * @date 18/11/2014 - 19:03:02
 */
public final class OperatingSystemLabel extends JLabel {
	
	private static final long serialVersionUID = -4548992718107864547L;
	
	private static final String ICON_FULL_NAME = "/org/jxSwing/resources/icon/so/:NAME-:SIZE.png";
	
	private ImageIcon ICON_32x32 = null;
	
	private ImageIcon ICON_64x64 = null;
	
	/**
	 * 
	 */
	public OperatingSystemLabel() {
	
		this(false);
	}
	
	/**
	 * @param showText
	 */
	public OperatingSystemLabel(boolean showText) {
	
		super();
		
		setToolTipText(OperatingSystem.getName() + " " + OperatingSystem.getVersion());
		
		//
		if(OperatingSystem.isArchLinux()) {
			
			ICON_32x32 = getImageIcon("archlinux", "32x32");
			ICON_64x64 = getImageIcon("archlinux", "64x64");
			
		}else if(OperatingSystem.isUbuntuLinux()) {
			
			ICON_32x32 = getImageIcon("ubuntu", "32x32");
			ICON_64x64 = getImageIcon("ubuntu", "64x64");
			
		}else if(OperatingSystem.isDebianLinux()) {
			
			ICON_32x32 = getImageIcon("debian", "32x32");
			ICON_64x64 = getImageIcon("debian", "64x64");
			
		}else if(OperatingSystem.isGentooLinux()) {
			
			ICON_32x32 = getImageIcon("gentoo", "32x32");
			ICON_64x64 = getImageIcon("gentoo", "64x64");
			
		}else if(OperatingSystem.isRedHatLinux()) {
			
			ICON_32x32 = getImageIcon("radhat", "32x32");
			ICON_64x64 = getImageIcon("radhat", "64x64");
			
		}else if(OperatingSystem.isSlackwareLinux()) {
			
			ICON_32x32 = getImageIcon("slackware", "32x32");
			ICON_64x64 = getImageIcon("slackware", "64x64");
			
		}else if(OperatingSystem.isSun()) {
			
			ICON_32x32 = getImageIcon("solaris", "32x32");
			ICON_64x64 = getImageIcon("solaris", "64x64");
			
		}else if(OperatingSystem.isSuSELinux()) {
			
			ICON_32x32 = getImageIcon("suse", "32x32");
			ICON_64x64 = getImageIcon("suse", "64x64");
			
		}else if(OperatingSystem.isWindows()) {
			
			setToolTipText(OperatingSystem.getName());
			
			ICON_32x32 = getImageIcon("windows", "32x32");
			ICON_64x64 = getImageIcon("windows", "64x64");
			
		}else if(OperatingSystem.isMac()) {
			
			setToolTipText(OperatingSystem.getName());
			
			ICON_32x32 = getImageIcon("mac", "32x32");
			ICON_64x64 = getImageIcon("mac", "64x64");
		}
		
		setIcon();
		
		// 
		if(showText) {
			
			setSize(150, 150);
			setToolTipText(null);
			setText("<html>" + OperatingSystem.getString().replace(SystemProperties.getLineSeparator(), "<br>") + "</html>");
			
		}else {
			
			setSize(25, 25);
		}
		
		//
		addComponentListener(new ComponentAdapter() {
			
			@Override
			public void componentResized(final ComponentEvent componentEvent) {
			
				setIcon();
			}
		});
	}
	
	/**
	 * 
	 */
	private void setIcon() {
	
		//
		if(ICON_32x32 != null || ICON_64x64 != null) {
			
			//
			if(getSize().getHeight() < 26 || getSize().getWidth() < 26) {
				
				setIcon(ICON_32x32 == null ? ICON_32x32 : ImageUtil.resizeImage(ICON_32x32, 20, 20));
				
			}else if(getSize().getHeight() < 32 || getSize().getWidth() < 32) {
				
				setIcon(ICON_32x32 == null ? ICON_32x32 : ImageUtil.resizeImage(ICON_32x32, 25, 25));
				
			}else if(getSize().getHeight() < 64 || getSize().getWidth() < 64) {
				
				setIcon(ICON_32x32);
				
			}else {
				
				setIcon(ICON_64x64);
			}
		}
	}
	
	/**
	 * @param iconName
	 *        Example: add
	 * @param iconSize
	 *        Example: 32x32
	 * @return fullIconName Example: /org/jxSwing/resources/icon/action/<b>add</b>-<b>32x32</b>.png
	 */
	private String getFullIconName(final String iconName, final String iconDimension) {
	
		return ICON_FULL_NAME.replace(":NAME", iconName).replace(":SIZE", iconDimension);
	}
	
	/**
	 * @param iconName
	 *        Example: add
	 * @param iconSize
	 *        Example: 32x32
	 * @return urlIconResource
	 */
	private URL getIconResource(final String iconName, final String iconDimension) {
	
		return OperatingSystemLabel.class.getResource(getFullIconName(iconName, iconDimension));
	}
	
	/**
	 * @param iconName
	 *        Example: add
	 * @param iconSize
	 *        Example: 32x32
	 * @return imageIcon
	 */
	private ImageIcon getImageIcon(final String iconName, final String iconDimension) {
	
		return new ImageIcon(getIconResource(iconName, iconDimension));
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setSize(int, int)
	 */
	@Override
	public void setSize(final int width, final int height) {
	
		final Dimension dimension = new Dimension(width, height);
		
		super.setSize(width, height);
		super.setPreferredSize(dimension);
		super.setMinimumSize(dimension);
		super.setMaximumSize(dimension);
	}
}
